// 用户信息mixins
import {getAction, getFileAccessHttpUrl, chooseImage, uploadAction, postAction} from '@/util/api';
import {showInfo} from "@/util/uniUtils";
export const userInfoMixins = {
    data(){
      return {
          // 用户信息（包含系统、完善、注册信息）
          userInfo: {},
          // 系统用户信息
          sysUser:{},
          // 用户完善信息
          userPerfect:{},
          // 用户注册信息
          userRegister:{}
      }
    },
    onShow() {
        // 获取缓存中的校友信息
        this.getStorageUserInfo();
    },
    methods: {
        /**
         * 获取缓存中的用户信息
         */
        getStorageUserInfo() {
            try {
                const userInfo = uni.getStorageSync('userInfo');
                if (userInfo) {
                    this.userInfo = userInfo;
                    // 解构得到对应的系统用户信息、完善信息、注册信息
                    let {sysUser, userPerfect, userRegister} = userInfo
                    this.sysUser = sysUser
                    this.userPerfect = userPerfect
                    this.userRegister = userRegister
                    return true;
                } else {
                    return false;
                }
            } catch (e) {
                console.log("用户缓存信息获取失败")
                return false;
            }
        },


        /**
         * 获取用户信息 --查询后台
         */
        async getUserInfo() {
            // 缓存中获取不到信息，从后台获取
            try {
                let {msg, code, data} = await getAction("/api/user/queryLoginUser")
                if (code === 200) {
                    this.userInfo = data;
                    // 清除缓存
                    uni.removeStorage('userInfo')
                    // 设置用户缓存信息
                    uni.setStorageSync('userInfo', data)
                    let {sysUser, userPerfect, userRegister} = data
                    this.sysUser = sysUser
                    this.userPerfect = userPerfect
                    this.userRegister = userRegister
                    if (sysUser.user && sysUser.user.avatar) {
                        // 设置头像可访问路径
                        this.sysUser.user.avatar = getFileAccessHttpUrl(sysUser.user.avatar)
                    }
                    return true;
                }
            } catch (e) {
                showInfo(e)
                return false;
            }
        },

        /**
         * 修改头像
         */
        async updateAvatar() {
            try {
                let tempPath = await chooseImage()
                // 获取到服务器返回地址
                let path = await uploadAction(tempPath)
                // 提交信息
                let sysUser = {userId: this.sysUser.userId, avatar: path}
                postAction('api/user/updateSysUserById', sysUser).then(result => {
                    if (result.code === 200) {
                        // 修改成功，重新刷新用户缓存
                        this.getUserInfo();
                        showInfo("修改成功")
                        return true
                    } else {
                        showInfo("修改失败")
                        return false
                    }
                })
            } catch (e) {
                showInfo("修改失败")
                return false
            }
        },
    }
}
